<?php
@ob_start();
@session_start();

include("config/connect.php");
include("function/thaidate.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylesheet/stylereport.css" type="text/css" />
<link rel="shortcut icon" href="image/shopping-bag.ico" />
<style type="text/css" media="print">

input{
	display:none;
}

</style>
</head>
<body>
<h1>ประวัติการซื้อ เฮือนคำ รีสอร์ท</h1>
<?php
if($_POST['rtype']=='day' && $_POST['dateInput']!=""){
	$dmy = $_POST["dateInput"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	//$date = ($y - 543) . '-' . $m . '-' . $d;
	//$date = date("Y-m-d", $adate);
	
	$date = $year."-".$month."-".$day;
	
	echo "<h2>ประจำ". thaidate(strtotime($date)) ."</h2>";
	
	$Query = mysql_query("SELECT * FROM buy_history WHERE buy_his_date = '$date' ");

/*
//แสดงรายงานเครื่องดื่มแอลกอฮอล์
	$adate = $year . '-' . $month . '-' . $day;
	
$Query = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND day(sale.sale_datetime3) = '$day' AND month(sale.sale_datetime3) = '$month' AND year(sale.sale_datetime3) = '$year' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");

}else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	
$Query = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = 	mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND month(sale.sale_datetime3) = '".$_POST['monthSelect']."' AND year(sale.sale_datetime3) = '".$_POST['yearSelect']."' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");

}else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
$Query = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '3' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query1 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '2' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query3 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '1' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
$Query4 = mysql_query("SELECT product.prod_name as prod_name, product.prodtype_id as prodtype_id ,sale.sale_price as sale_price, sum(sale.sale_qty) as sale_qty, sum(sale.sale_discount) as sale_discount FROM product,sale WHERE product.prod_id = sale.prod_id AND product.prodtype_id = '4' AND sale.sale_datetime3 BETWEEN '$date1' AND '$date2' GROUP BY sale.prod_id,sale.sale_price ORDER BY sum(sale.sale_qty) DESC");
*/
} else if($_POST['rtype']=='now' && $_POST['DateNow']!="") {

	$date = $_POST['DateNow'];
	
	echo "<h2>ประจำ". thaidate(strtotime($date)) ."</h2>";
	
	$Query = mysql_query("SELECT * FROM buy_history WHERE buy_his_date = '$date' ");
	
} else if($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!=""){
	
	$monthname = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
	$year = $_POST['yearSelect']+543;
	echo "<h2>ประจำเดือน ".$monthname[$_POST['monthSelect']-1]." พ.ศ. ". $year ."</h2>";
	
	$Query = mysql_query("SELECT * FROM buy_history WHERE month(buy_his_date) = '".$_POST['monthSelect']."' AND year(buy_his_date) = '".$_POST['yearSelect']."' ORDER BY buy_his_date ");
	
} else if($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!=""){
	
	$dmy = $_POST["dateInput1"];
	$day = substr($dmy, 0, 2);
	$month = substr($dmy, 3, 2);
	$y = substr($dmy, 6, 4);
	$year = $y-543;
	$date1 = $year."-".$month."-".$day;
	
	$dmy1 = $_POST["dateInput2"];
	$day1 = substr($dmy1, 0, 2);
	$month1 = substr($dmy1, 3, 2);
	$y1 = substr($dmy1, 6, 4);
	$year1 = $y1-543;
	$date2 = $year1."-".$month1."-".$day1;
	
	echo "<h2>ตั้งแต่วันที่ ".thai_date(strtotime($date1))." ถึงวันที่ ".thai_date(strtotime($date2))."</h2>";
	
	$Query = mysql_query("SELECT * FROM buy_history WHERE buy_his_date BETWEEN '$date1' AND '$date2' ORDER BY buy_his_date ");
}
	$numQuery = mysql_num_rows($Query);
?>

<?php 
	$i=0;
	while ($row=mysql_fetch_array($Query)){
		$i++;
?>
<br /><font size="4"><strong><?php echo $row['buy_his_bill']; ?></strong></font>
<?php if(($_POST['rtype']=='month' && $_POST['monthSelect']!="" && $_POST['yearSelect']!="") || ($_POST['rtype']=='dateto' && $_POST['dateInput1']!="" && $_POST['dateInput2']!="")){
	echo "<br /><font size=\"3\">วันที่ ".thai_date(strtotime($row['buy_his_date']))."</font>";
}
	?>
<br /><br />
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td>รายละเอียด</td>
<td width="16%">จำนวน</td>
<td width="16%">ราคาต่อหน่วย</td>
<td width="16%">จำนวนเงิน</td>
</tr>
  </thead>
<?php
		$sQuery = mysql_query("SELECT * FROM buy_history,buy_detail,product,category,unit WHERE buy_history.buy_his_id = buy_detail.buy_his_id AND buy_detail.product_id = product.product_id AND product.category_id = category.category_id AND product.unit_id = unit.unit_id AND buy_history.buy_his_id = '".$row['buy_his_id']."' ORDER BY buy_history.buy_his_id");
		$num1=0;
		while($sRow = mysql_fetch_array($sQuery)){
			$num1++;
?>
<tr>
	<td align="center"><?php echo $num1; ?></td>
	<td class="left" style="padding-left:12px"><?php echo $sRow['product_name']; ?></td>
	<td class="right" style="padding-right:12px"><?php echo number_format($sRow["qty"], 2,'.',',')." ".$sRow['unit_name'];?></td>
	<td class="right" style="padding-right:12px"><?php echo number_format($sRow["price"], 2,'.',',');?></td>
	<td class="right" style="padding-right:12px"><?php $total = $sRow["total"]; echo number_format($total, 2,'.',',');?></td>
</tr>
<?php
	$strTotal1[$i]=$strTotal1[$i]+$total;
}
?>
<tfoot>
  <tr>
    <td colspan="4" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal1[$i], 2,'.',','); ?></td>
	</tr>
</tfoot>
</table><br />
<?php
	$strtal = $strtal+$strTotal1[$i];
	}
	
		if($numQuery>='1'){
?>
<div class="endtotal">

รวมทั้งหมด 
<?php
	echo number_format($strtal, 2,'.',',')." บาท";
//echo "".number_format($strTotal1+$strTotal2+$strTotal3+$strTotal4, 2,'.',',')." บาท"; 
	} else {
		echo "ไม่มีประวัติการซื้อค่ะ";
	}
?></div>

<div style="padding-top: 20px">
<input type="button" name="endsale" id="endsale" value="พิมพ์รายงาน" onclick="print();" > <input type="button" name="btnClose" value="ปิดหน้าต่าง" onclick="JavaScript:window.close();" /></div>
</body>

</html>
